<script setup>
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { getOverviewApi } from "@/api/report";
import { ElMessage } from "element-plus";
import {
  Histogram,
  Collection,
  TrendCharts,
  DataLine,
  Finished,
  User,
  Notebook,
  Tickets,
  List,
} from "@element-plus/icons-vue";

const router = useRouter();

// 概览数据
const overviewData = ref({
  clueTotal: 0,
  clueWaitAllot: 0,
  clueWaitFollow: 0,
  clueFollowing: 0,
  clueFalse: 0,
  clueConvertBusiness: 0,
  businessTotal: 0,
  businessWaitAllot: 0,
  businessWaitFollow: 0,
  businessFollowing: 0,
  businessFalse: 0,
  businessConvertCustomer: 0,
});

// 获取概览数据
const getOverview = async () => {
  try {
    const result = await getOverviewApi();
    if (result.code === 1) {
      overviewData.value = result.data;
    } else {
      ElMessage.error(result.msg || "获取数据失败");
    }
  } catch (error) {
    ElMessage.error("网络错误，请稍后重试");
  }
};

// 页面跳转函数
const navigateTo = (path) => {
  router.push(path);
};

onMounted(() => {
  getOverview();
});
</script>

<template>
  <div class="home-container">
    <!-- 背景装饰 -->
    <div class="bg-bubble bubble-1" />
    <div class="bg-bubble bubble-2" />

    <!-- 欢迎标题 -->
    <div class="welcome-title">
      <h1><span class="grad-text">欢迎您</span>，进入轻客管家系统</h1>
      <p class="subtitle">Overview · Dashboard</p>
    </div>

    <!-- 概览面板 -->
    <div class="overview-section">
      <!-- 线索概览 -->
      <div class="overview-panel glass-card">
        <h2 class="panel-title">线索概览</h2>
        <div class="metrics-grid">
          <div class="metric-item primary">
            <div class="metric-icon">
              <el-icon><Histogram /></el-icon>
            </div>
            <div class="metric-number">{{ overviewData.clueTotal }}</div>
            <div class="metric-label">总线索数</div>
          </div>
          <div class="metric-item warning">
            <div class="metric-icon">
              <el-icon><List /></el-icon>
            </div>
            <div class="metric-number">{{ overviewData.clueWaitAllot }}</div>
            <div class="metric-label">待分配线索数</div>
          </div>
          <div class="metric-item info">
            <div class="metric-icon">
              <el-icon><TrendCharts /></el-icon>
            </div>
            <div class="metric-number">{{ overviewData.clueWaitFollow }}</div>
            <div class="metric-label">待跟进线索数</div>
          </div>
          <div class="metric-item success">
            <div class="metric-icon">
              <el-icon><Finished /></el-icon>
            </div>
            <div class="metric-number">{{ overviewData.clueFollowing }}</div>
            <div class="metric-label">跟进中线索数</div>
          </div>
          <div class="metric-item purple">
            <div class="metric-icon">
              <el-icon><Collection /></el-icon>
            </div>
            <div class="metric-number">{{ overviewData.clueFalse }}</div>
            <div class="metric-label">伪线索数</div>
          </div>
          <div class="metric-item danger">
            <div class="metric-icon">
              <el-icon><DataLine /></el-icon>
            </div>
            <div class="metric-number">
              {{ overviewData.clueConvertBusiness }}
            </div>
            <div class="metric-label">转商机线索数</div>
          </div>
        </div>
      </div>

      <!-- 商机概览 -->
      <div class="overview-panel glass-card">
        <h2 class="panel-title">商机概览</h2>
        <div class="metrics-grid">
          <div class="metric-item primary">
            <div class="metric-icon">
              <el-icon><Histogram /></el-icon>
            </div>
            <div class="metric-number">{{ overviewData.businessTotal }}</div>
            <div class="metric-label">总商机数</div>
          </div>
          <div class="metric-item warning">
            <div class="metric-icon">
              <el-icon><List /></el-icon>
            </div>
            <div class="metric-number">
              {{ overviewData.businessWaitAllot }}
            </div>
            <div class="metric-label">待分配商机数</div>
          </div>
          <div class="metric-item info">
            <div class="metric-icon">
              <el-icon><TrendCharts /></el-icon>
            </div>
            <div class="metric-number">
              {{ overviewData.businessWaitFollow }}
            </div>
            <div class="metric-label">待跟进商机数</div>
          </div>
          <div class="metric-item success">
            <div class="metric-icon">
              <el-icon><Finished /></el-icon>
            </div>
            <div class="metric-number">
              {{ overviewData.businessFollowing }}
            </div>
            <div class="metric-label">跟进中商机数</div>
          </div>
          <div class="metric-item danger">
            <div class="metric-icon">
              <el-icon><DataLine /></el-icon>
            </div>
            <div class="metric-number">{{ overviewData.businessFalse }}</div>
            <div class="metric-label">回收商机数</div>
          </div>
          <div class="metric-item purple">
            <div class="metric-icon">
              <el-icon><Collection /></el-icon>
            </div>
            <div class="metric-number">
              {{ overviewData.businessConvertCustomer }}
            </div>
            <div class="metric-label">转客户商机数</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 功能模块 -->
    <div class="modules-section">
      <div class="modules-grid">
        <div
          class="module-item gradient-border"
          @click="navigateTo('/resource/course')"
        >
          <div class="module-icon">
            <el-icon><Tickets /></el-icon>
          </div>
          <h3 class="module-title">课程管理</h3>
          <p class="module-subtitle">课程信息维护</p>
        </div>

        <div
          class="module-item gradient-border"
          @click="navigateTo('/resource/activity')"
        >
          <div class="module-icon">
            <el-icon><Collection /></el-icon>
          </div>
          <h3 class="module-title">活动管理</h3>
          <p class="module-subtitle">营销活动管理</p>
        </div>

        <div
          class="module-item gradient-border"
          @click="navigateTo('/clue/list')"
        >
          <div class="module-icon">
            <el-icon><TrendCharts /></el-icon>
          </div>
          <h3 class="module-title">线索管理</h3>
          <p class="module-subtitle">潜在客户追踪</p>
        </div>

        <div
          class="module-item gradient-border"
          @click="navigateTo('/business/list')"
        >
          <div class="module-icon">
            <el-icon><DataLine /></el-icon>
          </div>
          <h3 class="module-title">商机管理</h3>
          <p class="module-subtitle">销售机会管理</p>
        </div>

        <div
          class="module-item gradient-border"
          @click="navigateTo('/customer')"
        >
          <div class="module-icon">
            <el-icon><User /></el-icon>
          </div>
          <h3 class="module-title">客户管理</h3>
          <p class="module-subtitle">客户信息维护</p>
        </div>

        <div
          class="module-item gradient-border"
          @click="navigateTo('/system/log')"
        >
          <div class="module-icon">
            <el-icon><Notebook /></el-icon>
          </div>
          <h3 class="module-title">日志统计</h3>
          <p class="module-subtitle">系统操作日志</p>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.home-container {
  --c-primary: #409eff;
  --c-success: #67c23a;
  --c-warning: #e6a23c;
  --c-danger: #f56c6c;
  --c-info: #909399;
  --c-purple: #a56cff;

  position: relative;
  padding: 28px 24px 40px;
  background: radial-gradient(
      1200px 600px at 10% -20%,
      #e8f1ff 0%,
      transparent 60%
    ),
    radial-gradient(1200px 600px at 90% 0%, #ffeef3 0%, transparent 60%),
    #f6f7fb;
  min-height: 100vh;
}

.bg-bubble {
  position: absolute;
  border-radius: 50%;
  filter: blur(50px);
  opacity: 0.5;
  z-index: 0;
}
.bubble-1 {
  width: 220px;
  height: 220px;
  left: -60px;
  top: 40px;
  background: #b6d5ff;
}
.bubble-2 {
  width: 260px;
  height: 260px;
  right: -80px;
  top: 120px;
  background: #ffd2e0;
}

.welcome-title {
  text-align: center;
  margin-bottom: 24px;
  position: relative;
  z-index: 1;
}
.welcome-title h1 {
  color: #333;
  font-size: 30px;
  font-weight: 600;
  margin: 0;
  letter-spacing: 0.5px;
}
.welcome-title .grad-text {
  background: linear-gradient(90deg, #5b8cff, #9b5bff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.subtitle {
  margin-top: 6px;
  color: #8a8f99;
  font-size: 12px;
}

.overview-section {
  display: flex;
  gap: 20px;
  margin-bottom: 26px;
  position: relative;
  z-index: 1;
}

.glass-card {
  background: rgba(255, 255, 255, 0.75);
  border: 1px solid rgba(255, 255, 255, 0.6);
  box-shadow: 0 10px 30px rgba(96, 107, 140, 0.15);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.overview-panel {
  flex: 1;
  border-radius: 14px;
  padding: 18px 18px 20px;
}
.panel-title {
  color: #333;
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 16px 0;
  text-align: center;
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
}

.metric-item {
  --accent: var(--c-primary);
  position: relative;
  text-align: center;
  padding: 16px 14px 14px;
  border-radius: 12px;
  border: 1px solid rgba(0, 0, 0, 0.06);
  background: linear-gradient(
    180deg,
    rgba(255, 255, 255, 0.95),
    rgba(255, 255, 255, 0.84)
  );
  box-shadow: 0 6px 16px rgba(52, 63, 87, 0.08);
  transition: transform 0.25s ease, box-shadow 0.25s ease;
}
.metric-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 14px 28px rgba(64, 158, 255, 0.15),
    0 0 0 2px var(--accent) inset;
}
.metric-icon {
  width: 36px;
  height: 36px;
  margin: 0 auto 8px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  background: var(--accent);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.08),
    0 0 0 6px color-mix(in oklab, var(--accent) 20%, transparent);
}
.metric-number {
  font-size: 26px;
  font-weight: 800;
  color: var(--accent);
  line-height: 1;
}
.metric-label {
  font-size: 12px;
  color: #6b7280;
  margin-top: 6px;
  line-height: 1.2;
}

.metric-item.primary {
  --accent: var(--c-primary);
}
.metric-item.success {
  --accent: var(--c-success);
}
.metric-item.warning {
  --accent: var(--c-warning);
}
.metric-item.danger {
  --accent: var(--c-danger);
}
.metric-item.info {
  --accent: var(--c-info);
}
.metric-item.purple {
  --accent: var(--c-purple);
}

.modules-section {
  margin-top: 6px;
  position: relative;
  z-index: 1;
}
.modules-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 18px;
}

.module-item {
  position: relative;
  background: #fff;
  border-radius: 14px;
  padding: 22px 18px;
  text-align: center;
  cursor: pointer;
  transition: transform 0.25s ease, box-shadow 0.25s ease;
  box-shadow: 0 8px 20px rgba(52, 63, 87, 0.08);
}
.module-item:hover {
  transform: translateY(-4px);
  box-shadow: 0 14px 30px rgba(52, 63, 87, 0.16);
}

.gradient-border {
  border: 1px solid transparent;
  background: linear-gradient(#ffffff, #ffffff) padding-box,
    linear-gradient(135deg, #5b8cff, #9b5bff) border-box;
}
.module-icon {
  width: 42px;
  height: 42px;
  margin: 0 auto 10px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  background: linear-gradient(135deg, #5b8cff, #9b5bff);
  box-shadow: 0 8px 16px rgba(155, 91, 255, 0.25);
}
.module-title {
  color: #333;
  font-size: 17px;
  font-weight: 600;
  margin: 0 0 6px 0;
}
.module-subtitle {
  color: #7a7f8a;
  font-size: 13px;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .overview-section {
    flex-direction: column;
  }
  .modules-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
@media (max-width: 768px) {
  .metrics-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .modules-grid {
    grid-template-columns: 1fr;
  }
  .welcome-title h1 {
    font-size: 24px;
  }
}
</style>
